package gw.demo2.controller;

import gw.demo2.entity.PurchaseRecord;
import gw.demo2.service.PurchaseRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/purchase")
public class PurchaseRecordController {
    @Autowired
    private PurchaseRecordService purchaseRecordService;

    @PostMapping
    public ResponseEntity<?> addPurchaseRecord(@RequestBody PurchaseRecord record) {
        if (purchaseRecordService.addPurchaseRecord(record)) {
            return ResponseEntity.ok().body("Purchase record added successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add purchase record");
        }
    }

    @GetMapping
    public ResponseEntity<?> getPurchaseRecords(@RequestParam int userId) {
        return ResponseEntity.ok().body(purchaseRecordService.getPurchaseRecordsByUserId(userId));
    }
}